﻿using System;
using System.Runtime.Serialization;

namespace RockBus.DataContracts
{
    [DataContract(Namespace = StringConstants.RockBusNamespace)]
    public enum IsolationLevelEnum
    {
        // Summary:
        //     Volatile data can be read but not modified, and no new data can be added
        //     during the transaction.
        [EnumMember]
        Serializable = 0,
        //
        // Summary:
        //     Volatile data can be read but not modified during the transaction. New data
        //     can be added during the transaction.
        [EnumMember]
        RepeatableRead = 1,
        //
        // Summary:
        //     Volatile data cannot be read during the transaction, but can be modified.
        [EnumMember]
        ReadCommitted = 2,
        //
        // Summary:
        //     Volatile data can be read and modified during the transaction.
        [EnumMember]
        ReadUncommitted = 3,
        //
        // Summary:
        //     Volatile data can be read. Before a transaction modifies data, it verifies
        //     if another transaction has changed the data after it was initially read.
        //     If the data has been updated, an error is raised. This allows a transaction
        //     to get to the previously committed value of the data.
        [EnumMember]
        Snapshot = 4,
        //
        // Summary:
        //     The pending changes from more highly isolated transactions cannot be overwritten.
        [EnumMember]
        Chaos = 5,
        //
        // Summary:
        //     A different isolation level than the one specified is being used, but the
        //     level cannot be determined. An exception is thrown if this value is set.
        [EnumMember]
        Unspecified = 6,
    }

    [DataContract(Namespace = StringConstants.RockBusNamespace)]
    public class InternalConfiguration
    {
        public static InternalConfiguration CreateDefaultInternalConfiguration()
        {
            int defaultTimeoutMs = 60000;
            int defaultTransientMaxAttemptCount = 3;
            return new InternalConfiguration()
            {
                CorrelationTimeoutMs = defaultTimeoutMs,
                DebugMessageLoggingEnabled = false,
                DefaultMaxMessageBufferSize = 4 * 1024 * 1024,
                DefaultTransientDeliveryAttemptIntervalSecs = defaultTimeoutMs / (defaultTransientMaxAttemptCount * 1000),
                DefaultTransientMaxDeliveryAttemptCount = defaultTransientMaxAttemptCount,
                IsolationLevel = IsolationLevelEnum.ReadCommitted,
                MaxDegreeOfParallelism = Environment.ProcessorCount,
                MessageTimeoutMs = defaultTimeoutMs,
                PerformanceCounterUpdateIntervalMs = 2000,
                ReinitializationIntervalMs = defaultTimeoutMs,
                ReinitLockTimeoutMs = defaultTimeoutMs,
                ServiceHostOpenTimeoutMs = defaultTimeoutMs,
                TransactionTimeoutMs = defaultTimeoutMs,
                BufferManagerMaxBufferSize = 32768,
                BufferManagerMaxPoolSize = 100,
                DefaultPersistentDeliveryAttemptIntervalSecs = 30000,
                DefaultPersistentMaxDeliveryAttemptCount = 3,
                DeliveryTimeoutMs = 60000,
                MessageFragmentSize = 32768,
                PollingBatchSize = 100,
                PollingIntervalMs = 500,
            };
        }

        [DataMember]
        public int ReinitializationIntervalMs { get; set; }

        [DataMember]
        public int ReinitLockTimeoutMs { get; set; }

        [DataMember]
        public int CorrelationTimeoutMs { get; set; }

        [DataMember]
        public int PerformanceCounterUpdateIntervalMs { get; set; }

        [DataMember]
        public int MessageTimeoutMs { get; set; }

        [DataMember]
        public int ServiceHostOpenTimeoutMs { get; set; }

        [DataMember]
        public int MaxDegreeOfParallelism { get; set; }

        [DataMember]
        public IsolationLevelEnum IsolationLevel { get; set; }

        [DataMember]
        public int TransactionTimeoutMs { get; set; }

        [DataMember]
        public int DefaultTransientDeliveryAttemptIntervalSecs { get; set; }

        [DataMember]
        public int DefaultTransientMaxDeliveryAttemptCount { get; set; }

        [DataMember]
        public int DefaultMaxMessageBufferSize { get; set; }

        [DataMember]
        public bool DebugMessageLoggingEnabled { get; set; }

        [DataMember]
        public int BufferManagerMaxPoolSize { get; set; }

        [DataMember]
        public int BufferManagerMaxBufferSize { get; set; }

        [DataMember]
        public int MessageFragmentSize { get; set; }

        [DataMember]
        public int PollingBatchSize { get; set; }

        [DataMember]
        public int PollingIntervalMs { get; set; }

        [DataMember]
        public int DeliveryTimeoutMs { get; set; }

        [DataMember]
        public int DefaultPersistentDeliveryAttemptIntervalSecs { get; set; }

        [DataMember]
        public int DefaultPersistentMaxDeliveryAttemptCount { get; set; }

        [DataMember]
        public string ContentFolder { get; set; }

        [DataMember]
        public string CustomAssemblyFolder { get; set; }

        [DataMember]
        public EndpointInfo RequestPersistenceEndpoint { get; set; }

        [DataMember]
        public EndpointInfo ResponsePersistenceEndpoint { get; set; }
    }
}
